//
//  FYStatusOriginView.swift
//  Mircore
//
//  Created by apple on 16/8/12.
//  Copyright © 2016年 飞羽. All rights reserved.
//

import UIKit

import SnapKit


import  SDWebImage

//原创微博

class FYStatusOriginView: UIView {
    
    //记录原创微博底部约束
    
    var  originViewConstraint: Constraint?
    
    var  statusViewModel: FYStatusViewModel? {
        
        didSet  {
            
            //设置控件的数据
            
            if  let profile  = statusViewModel?.status?.user?.profile_image_url {
                
                //绑定头像数据
                
                headImageView.sd_setImageWithURL(NSURL(string: profile),placeholderImage: UIImage(named: "avatar_default_big"))
                
            }
            
            //绑定昵称
            
            screenNameLabel.text  = statusViewModel?.status?.user?.screen_name
            
            //设置微博数据
            //绑定原创微博富文本
            contentLabel.attributedText  = statusViewModel?.origianAttributedString
            
            //来源
            
            sourceLabel.text  = statusViewModel?.sourceContent
            
            
            //设置会员等级图片
            
            mbrankImageView.image  = statusViewModel?.mbrankImage
            
            //设置认证类型等级图片
            
            verifiedTypeImageView.image  = statusViewModel?.verifiedTypeImage
            
            //设置时间
            
            timeLabel.text  =  statusViewModel?.timeContent
            
            
            //卸载上次约束
            
            originViewConstraint?.uninstall()
            
            //判断原创微博的配图个数是否大于0
            
            if  let  picUrls  = statusViewModel?.status?.pic_urls  where  picUrls.count > 0 {
                
                
                //显示配图
                
                pictureView.hidden  = false
                
                //绑定原创微博数据源
                pictureView.picUrls  = picUrls
                
                
                //更新约束
                
                self.snp_updateConstraints(closure: { (make) in
                    
                    self.originViewConstraint  = make.bottom.equalTo(pictureView).offset(StatusTableViewCellMargin).constraint
                })
                
                
            }else  {
                
                //隐藏配图
                
                pictureView.hidden  = true
                
                self.snp_updateConstraints(closure: { (make) in
                    
                    self.originViewConstraint  = make.bottom.equalTo(contentLabel).offset(StatusTableViewCellMargin).constraint
                })
            }
            
        }
        
    }
    
    //MARK:- 懒加载控件
    
    //头像
    
    private  lazy var  headImageView: UIImageView  = {
        
        let  image  = UIImageView(image: UIImage(named: "avatar_default_big"))
        
        image.layer.cornerRadius = 17
        
        
        image.layer.masksToBounds  = true
        
        return  image
    }()
    
    
    
    //认证类型等级图片
    
    private  lazy  var  verifiedTypeImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    
    //昵称
    
    private  lazy  var  screenNameLabel: UILabel  =  UILabel(fontSize: 15, textColor: UIColor.darkGrayColor())
    
    //会员等级
    
    private  lazy  var mbrankImageView: UIImageView  = UIImageView(image: UIImage(named: "common_icon_membership"))
    
    //时间
    
    private  lazy  var  timeLabel  = UILabel(fontSize: 12, textColor: UIColor.orangeColor())
    
    //来源
    
    private lazy  var  sourceLabel  = UILabel(fontSize: 12, textColor: UIColor.lightGrayColor())
    
    //微博内容
    
    private lazy var  contentLabel : UILabel  = {
        
        let  label  = UILabel(fontSize: StatusFontSize, textColor: UIColor.darkGrayColor())
        
        //多行显示
        
        label.numberOfLines  = 0
        
        return label
        
    }()
    
    //配图
    
    private  lazy  var  pictureView: FYStatusPictureView = {
        
        let  view  = FYStatusPictureView()
        
        view.backgroundColor  = self.backgroundColor
        
        return view
    }()
    
    
    override  init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setupUI()
    }
    
    required  init?(coder aDecoder: NSCoder) {
        
        fatalError("init(coder:) has  not been  implemented")
    }
    
    //添加控件设置约束
    
    private  func  setupUI()  {
        
        //添加控件
        
        addSubview(headImageView)
        
        addSubview(verifiedTypeImageView)
        
        addSubview(screenNameLabel)
        
        addSubview(mbrankImageView)
        
        addSubview(timeLabel)
        
        addSubview(sourceLabel)
        
        addSubview(contentLabel)
        
        addSubview(pictureView)
        
         
        //设置约束
        
        headImageView.snp_makeConstraints { (make) in
            
            make.top.equalTo(self).offset(StatusTableViewCellMargin)
            
            make.leading.equalTo(self).offset(StatusTableViewCellMargin)
            
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        verifiedTypeImageView.snp_makeConstraints { (make) in
            
            make.centerX.equalTo(headImageView.snp_trailing)
            
            make.centerY.equalTo(headImageView.snp_bottom)
        }
        screenNameLabel.snp_makeConstraints { (make) in
            
            make.leading.equalTo(headImageView.snp_trailing).offset(StatusTableViewCellMargin)
            
            make.top.equalTo(headImageView)
            
        }
        
        mbrankImageView.snp_makeConstraints { (make) in
            
            make.leading.equalTo(screenNameLabel.snp_trailing).offset(StatusTableViewCellMargin)
            make.top.equalTo(screenNameLabel)
        }
        
        timeLabel.snp_makeConstraints { (make) in
            
            make.leading.equalTo(screenNameLabel)
            make.bottom.equalTo(headImageView)
        }
        
        sourceLabel.snp_makeConstraints { (make) in
            
            make.bottom.equalTo(headImageView)
            
            make.leading.equalTo(timeLabel.snp_trailing).offset(StatusTableViewCellMargin)
        }
        
        contentLabel.snp_makeConstraints { (make) in
            
            make.leading.equalTo(headImageView)
            
            make.top.equalTo(headImageView.snp_bottom).offset(StatusTableViewCellMargin)
            
            make.trailing.equalTo(self).offset(-StatusTableViewCellMargin)
        }
        
        pictureView.snp_makeConstraints { (make) in
            
            make.top.equalTo(contentLabel.snp_bottom).offset(StatusTableViewCellMargin)
            
            make.leading.equalTo(contentLabel)
        }
        
        //关键约束  原创微博视图的底部约束   = 微博内容的底部约束 + 间距
        
        self.snp_makeConstraints { (make) in
            
            self.originViewConstraint  =  make.bottom.equalTo(pictureView).offset(StatusTableViewCellMargin).constraint
        }
        
    }
}
